#! /usr/bin/python
# -*- coding: utf-8 -*-
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


class DataFileReader:
  """
  DataFileReader Class
     This class is an appropriate interface to read data files.
     Ignores commentaries and tell the line and column number of the data
   you're reading. Also, the number of columns on that line.
  _________________________________________________________
  A.Z. - 04/07 => Creation from my C++ class
         05/07 => Fixed the bug which made lines like:
                    #O commentary together with another character
                  don't be considered commentary lines
  """
    
  def __init__(self):
    """
    Constructor doesn't do anything
    """
    pass


  def read(self,dataFile,commentarySymbol="#"):
    """
    Read the file an return the non commented parts.
    """
    content = []

    DATAFILE = open(dataFile,'r')
    for line in DATAFILE:
      columns = line.split()

      # If the line is not empty, go inside
      if len(columns)>0:

        # Try to find the commentary
        try:
          commentaryPosition = columns.index(commentarySymbol)
          # If there is something before the commantary, read it
          if commentaryPosition!=0:  
            content.append(columns[:commentaryPosition])

        # Whitout commentary, read everything
        except ValueError:
          firstCharacter=(columns[0])[0]
          if firstCharacter!=commentarySymbol:
            content.append(columns)

    DATAFILE.close()
    return content
